<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="100px">
			<el-form-item label="项目名称" prop="itemName">
				<el-input v-model="queryParams.itemName" placeholder="请输入项目名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="是否可升级" prop="isCanUpgrade">
				<el-select v-model="queryParams.isCanUpgrade" placeholder="请选择是否可升级" clearable>
					<el-option v-for="dict in dict.type.is_can_upgrade" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="上架状态" prop="isShelves">
				<el-select v-model="queryParams.isShelves" placeholder="请选择上架状态" clearable>
					<el-option v-for="dict in dict.type.is_shelves" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['routineItem:routineItem:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['routineItem:routineItem:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['routineItem:routineItem:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['routineItem:routineItem:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="routineItemList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="ID" align="center" prop="id" />
			<el-table-column label="项目名称" align="center" prop="itemName" />
			<el-table-column label="项目主图" align="center" prop="itemMainPicture" width="100">
				<template slot-scope="scope">
					<image-preview :src="scope.row.itemMainPicture" :width="50" :height="50" />
				</template>
			</el-table-column>
			<el-table-column label="项目轮播图" align="center" prop="itemBanner" width="100">
				<template slot-scope="scope">
					<image-preview :src="scope.row.itemBanner" :width="50" :height="50" />
				</template>
			</el-table-column>
			<el-table-column label="项目时长" align="center" prop="itemTime">
				<template slot-scope="scope">
					{{scope.row.itemTime}}分钟
				</template>
			</el-table-column>
			<el-table-column label="项目价格" align="center" prop="itemPrice" />
			<el-table-column label="项目会员价" align="center" prop="itemMemberPrice" />
			<el-table-column label="是否可升级" align="center" prop="isCanUpgrade">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.is_can_upgrade" :value="scope.row.isCanUpgrade" />
				</template>
			</el-table-column>
			<el-table-column label="上架状态" align="center" prop="isShelves">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.is_shelves" :value="scope.row.isShelves" />
				</template>
			</el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.createTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['routineItem:routineItem:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['routineItem:routineItem:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改服务项目对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="项目名称" prop="itemName">
					<el-input v-model="form.itemName" placeholder="请输入项目名称" />
				</el-form-item>
				<el-form-item label="项目主图" prop="itemMainPicture">
					<image-upload v-model="form.itemMainPicture" />
				</el-form-item>
				<el-form-item label="项目轮播图" prop="itemBanner">
					<image-upload v-model="form.itemBanner" />
				</el-form-item>
				<el-form-item label="项目时长" prop="itemTime">
					<el-input v-model="form.itemTime" placeholder="请输入项目时长" />
				</el-form-item>
				<el-form-item label="项目简介" prop="itemIntroduction">
					<el-input v-model="form.itemIntroduction" placeholder="请输入项目简介" />
				</el-form-item>
				<el-form-item label="项目特色">
					<editor v-model="form.itemFeatures" :min-height="192" />
				</el-form-item>
				<el-form-item label="项目描述">
					<editor v-model="form.itemDescription" :min-height="192" />
				</el-form-item>
				<el-form-item label="购买量" prop="realSalesCount">
					<el-input v-model="form.realSalesCount" placeholder="请输入购买量" />
				</el-form-item>
				<el-form-item label="购买量" prop="virtualSalesCount">
					<el-input v-model="form.virtualSalesCount" placeholder="请输入购买量" />
				</el-form-item>
				<el-form-item label="项目价格" prop="itemPrice">
					<el-input v-model="form.itemPrice" placeholder="请输入项目价格" />
				</el-form-item>
				<el-form-item label="项目会员价" prop="itemMemberPrice">
					<el-input v-model="form.itemMemberPrice" placeholder="请输入项目会员价" />
				</el-form-item>
				<el-form-item label="是否可升级" prop="isCanUpgrade">
					<el-radio-group v-model="form.isCanUpgrade">
						<el-radio v-for="dict in dict.type.is_can_upgrade" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="上架状态" prop="isShelves">
					<el-radio-group v-model="form.isShelves">
						<el-radio v-for="dict in dict.type.is_shelves" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listRoutineItem,
		getRoutineItem,
		delRoutineItem,
		addRoutineItem,
		updateRoutineItem
	} from "@/api/routineItem/routineItem";

	export default {
		name: "RoutineItem",
		dicts: ['is_can_upgrade', 'is_shelves'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 服务项目表格数据
				routineItemList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					itemName: null,
					isCanUpgrade: null,
					isShelves: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询服务项目列表 */
			getList() {
				this.loading = true;
				listRoutineItem(this.queryParams).then(response => {
					this.routineItemList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					itemName: null,
					itemMainPicture: null,
					itemBanner: null,
					itemTime: null,
					itemIntroduction: null,
					itemFeatures: null,
					itemDescription: null,
					realSalesCount: null,
					virtualSalesCount: null,
					tid: [],
					itemPrice: null,
					itemMemberPrice: null,
					isCanUpgrade: null,
					isShelves: null,
					isDelete: null,
					createTime: null,
					updateTime: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.$commonJs.changeView('/routineItem/routineItem/addRoutine')
				// this.open = true;
				// this.title = "添加服务项目";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				// this.reset();
				// const id = row.id || this.ids
				// getRoutineItem(id).then(response => {
				//   this.form = response.data;
				//   this.form.tid = this.form.tid.split(",");
				//   this.open = true;
				//   this.title = "修改服务项目";
				// });
				this.reset();
				const id = row.id || this.ids
				this.$commonJs.changeView('/routineItem/routineItem/edit', {
					id: id
				})
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.form.tid = this.form.tid.join(",");
						if (this.form.id != null) {
							updateRoutineItem(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addRoutineItem(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除服务项目编号为"' + ids + '"的数据项？').then(function() {
					return delRoutineItem(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('routineItem/routineItem/export', {
					...this.queryParams
				}, `routineItem_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>